package com.itheima.controller.user;

import com.github.pagehelper.PageInfo;
import com.itheima.controller.BaseServlet;
import com.itheima.domain.Role;
import com.itheima.domain.UserInfo;
import com.itheima.utils.BeanUtil;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;
import java.util.UUID;

@WebServlet("/user/*")
public class UserServlet extends BaseServlet {
    //1.分页遍历用户
    private void list(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<UserInfo> list= null;
        PageInfo<UserInfo> pageInfo=null;

        //获取分页参数
        //当前页
        String pageNum = req.getParameter("pageNum");
        //当前页条数
        String pageSize = req.getParameter("pageSize");
        //读取来自网页的模糊查询请求参数
        String name = req.getParameter("name");
        if (name!=null){
            //1.模糊请求参数非空,进行模糊查询逻辑
            list = userService.findByName(name);
            //创建分页功能对象
            req.setAttribute("name",name);
        }else{
            //2.模糊请求参数为空,进行分页遍历查询逻辑
            list=userService.list(pageNum,pageSize);
        }
        pageInfo= new PageInfo<>(list);
        System.out.println(pageInfo);
        //设置分页请求参数
        req.setAttribute("page",pageInfo);
        //跳转用户列表界面
        req.getRequestDispatcher("/pages/user/user-list.jsp").forward(req, resp);
    }

    //2.遍历用户
    private void findAll(HttpServletRequest req, HttpServletResponse resp){
        //调用服务端方法获取UserInfo集合
        List<UserInfo> userList = userService.findAll();
        //设置请求头参数
        req.setAttribute("userList",userList);
    }

    //3.存储单个用户
    private void save(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //调用BeanUtil工具获取封装起来了的用户对象
        UserInfo user = BeanUtil.fillBean(req, UserInfo.class, "yyyy-MM-dd");
        //设定不重复id
        user.setId(UUID.randomUUID().toString().replaceAll("-", ""));
        //调用服务端方法存储单个用户
        userService.save(user);
        //跳转用户列表界面
        list(req,resp);
    }

    // 4.根据id删除指定用户(单个/多个)
    private void deleteByIds(HttpServletRequest req, HttpServletResponse resp) throws IOException, ServletException {
        String ids = req.getParameter("ids");
        //调用服务端方法删除指定用户,并返回删除结果(成功true/失败false)
        boolean flag = userService.deleteByIds(ids);
        //删除失败
        //封装flag为jason对象
        String jasonB = mapper.writeValueAsString(flag);
        //传递jason对象,在前端页面显示删除结果(删除成功/失败)
        resp.getWriter().write(jasonB);
    }

    //5.根据id查询单个用户
    private void findById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取要查询的用户id
        String id = req.getParameter("id");
        //调用服务端方法根据id查询指定用户
        UserInfo user = userService.findById(id);
        //设置请求头参数
        req.setAttribute("user",user);
    }

    //8.修改用户状态
    private void changeStatusById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //获取id
        String id = req.getParameter("id");
        //根据id修改指定用户状态
        userService.changeStatusById(id);
        //刷新页面
        list(req,resp);
    }

    //9.根据用户id查角色
    private void findRoleById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        UserInfo user = userService.findRoleById(id);
        Role role = user.getRole();
        req.setAttribute("role",role);
        req.getRequestDispatcher("/pages/user/user-role-add.jsp").forward(req, resp);
    }

    //10.获取全部角色信息
    private void findAllRole(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        List<Role> list = roleService.list("1","5");
        PageInfo<Role> roleList = new PageInfo<>(list);
        String uid = req.getParameter("uid");
        req.setAttribute("roleList",roleList);
        req.getSession().setAttribute("uid",uid);
        req.getRequestDispatcher("/pages/user/user-role-add.jsp").forward(req, resp);
    }

    //11.更改用户角色
    private void changeRoleById(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        String uid = req.getParameter("uid");
        userService.changeRoleById(id,uid);
        System.out.println("id="+id+", uid="+uid);
/*        userService.changeRoleById(id);
        list(req,resp);*/
    }

    //13.回显用户信息到用户信息详情界面
    private void showUserDesc(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        String id = req.getParameter("id");
        System.out.println(id);
        UserInfo user = userService.findById(id);
        System.out.println(user);
        req.setAttribute("user",user);
        req.getRequestDispatcher("/pages/user/user-show.jsp").forward(req, resp);
    }

    //13.查询用户的权限信息url字段
   /* private void findAllMes(HttpServletRequest req, HttpServletResponse resp,){

        List<String> list = userService.findPerById("1");
        System.out.println(list);
    }*/
}
